﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace Htss.Platform.WebApi
{
    public class Verify
    {
        private List<VerifyField> _verifyField = new List<VerifyField>();
        /// <summary>
        /// 验证结果
        /// </summary>
        public bool IsVerify { get; set; }
        /// <summary>
        /// 验证返回信息
        /// </summary>
        public string VerifyMsg { get; set; }
        /// <summary>
        /// 验证信息返回Dt类型
        /// </summary>
        public DataTable VerifyDt { get; set; }

        public Verify() { }
        /// <summary>
        /// 创建新验证字段
        /// </summary>
        /// <returns></returns>
        public VerifyField NewVerifyField()
        {
            VerifyField field = new VerifyField();
            _verifyField.Add(field);
            return field;
        }
        /// <summary>
        /// 创建新验证字段
        /// </summary>
        /// <param name="inputText">需要验证的字段值</param>
        /// <param name="fieldName">需要验证的字段名</param>
        /// <returns></returns>
        public VerifyField NewVerifyField(string inputText,string fieldName)
        {
            VerifyField field = new VerifyField(inputText, fieldName);
            _verifyField.Add(field);
            return field;
        }
        /// <summary>
        /// 开始验证
        /// </summary>
        public void Begin()
        {

            if (_verifyField != null && _verifyField.Count > 0)
            {
                //存储没有通过验证时的返回信息
                DataTable dt = new DataTable();

                DataColumn dc = new DataColumn();
                dc.ColumnName = "FieldName";
                dt.Columns.Add(dc);

                DataColumn dc1 = new DataColumn();
                dc1.ColumnName = "ErrorText";
                dt.Columns.Add(dc1);

                InputCheck check = new InputCheck();
                bool bo;//记录单验证项是否成功
                foreach (VerifyField vf in _verifyField)
                {
                    bo = true;
                    DataRow dr = dt.NewRow();
                    foreach (VerifyItem vi in vf.itemList)
                    {
                        switch (vi.VerifyType)
                        {
                            case VerifyType.Null:
                                if (!check.IsNull(vf.InputText)){
                                    dr["FieldName"] = vf.FieldName;
                                    dr["ErrorText"] = vi.ErrorText;
                                    
                                    bo = false;
                                }
                                break;
                            case VerifyType.LengthRange:
                                if (!check.IsLengthRange(vf.InputText, vi.MinLength, vi.MaxLength))
                                {
                                    dr["FieldName"] = vf.FieldName;
                                    dr["ErrorText"] = vi.ErrorText;
                                    
                                    bo = false;
                                }
                                break;
                            case VerifyType.Compare:
                                if (!check.IsCompare(vf.InputText, vi.CompareText)) {
                                    dr["FieldName"] = vf.FieldName;
                                    dr["ErrorText"] = vi.ErrorText;
                                    
                                    bo = false;
                                }
                                break;
                            case VerifyType.Regex:
                                if (!check.IsRegex(vf.InputText, vi.RegexText))
                                {
                                    dr["FieldName"] = vf.FieldName;
                                    dr["ErrorText"] = vi.ErrorText;
                                    
                                    bo = false;
                                }
                                break;
                            case VerifyType.Custom:
                                if (!vi.Custom) {
                                    dr["FieldName"] = vf.FieldName;
                                    dr["ErrorText"] = vi.ErrorText;
                                    
                                    bo = false;
                                }
                                break;
                        }
                        //当其中有一项验证不通过时则跳出循环
                        if (!bo)
                        {
                            break;
                        }
                    }
                    if (!bo)
                    {
                        dt.Rows.Add(dr);
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    IsVerify = false;
                    VerifyMsg = Htss.Platform.Core.Json.ToJson(dt);
                    VerifyDt = dt;
                }
                else
                {
                    IsVerify = true;
                }
            }
            else
            {
                IsVerify = true;
            }
        }
    }
}
